<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:lxslt="http://xml.apache.org/xslt"
                xmlns:redirect="http://xml.apache.org/xalan/redirect"
                extension-element-prefixes="redirect">

    <xsl:template match="error">
        <xsl:variable name="source" select="@source"/>
        <xsl:if test="count(preceding::error[@source=$source]) = 0">
            <redirect:write file="{$output.dir}/files/{$source}.html">

                <html>
                    <head>
                        <link rel="stylesheet" type="text/css">
                            <xsl:attribute name="href">
                                <xsl:text>../stylesheet.css</xsl:text>
                            </xsl:attribute>
                        </link>
                    </head>
                    <body class="mainframe">
                        <xsl:call-template name="pageHeader"/>
                        <h3>Error
                            <xsl:value-of select="$source"/>
                            (Severity: <xsl:value-of select="@severity"/>,
                            Files: <xsl:value-of select="count(//file[count(./error[@source=$source])>0])"/>,
                            Errors: <xsl:value-of select="count(//error[@source=$source])"/>)
                        </h3>

                        <ul>
                            <xsl:apply-templates select="/checkstyle/checktype" mode="error.checktype">
                                <xsl:with-param name="error" select="$source"/>
                                <xsl:sort select="@type"/>
                            </xsl:apply-templates>
                        </ul>
                    </body>
                </html>
            </redirect:write>
        </xsl:if>

    </xsl:template>

    <xsl:template match="checktype" mode="error.checktype">
        <xsl:param name="error"/>
        <xsl:variable name="fileCount" select="count(.//file[count(./error[@source=$error])>0])"/>

        <xsl:if test="$fileCount > 0">
            <li class="checktype">
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="@type"/>
                        <xsl:text>.html</xsl:text>
                    </xsl:attribute>
                    <xsl:value-of select="@type"/>
                </a>
                (files: <xsl:value-of select="$fileCount"/>, errors: <xsl:value-of select="count(.//error[@source=$error])"/>)
                <ul>
                    <xsl:apply-templates select="folder" mode="error.folder">
                        <xsl:with-param name="error" select="$error"/>
                        <xsl:with-param name="parent-folder" select="''"/>
                    </xsl:apply-templates>
                </ul>
            </li>
        </xsl:if>
    </xsl:template>

    <xsl:template match="folder" mode="error.folder">
        <xsl:param name="error"/>
        <xsl:param name="parent-folder"/>
        <xsl:variable name="new-name">
            <xsl:call-template name="from-dos">
                <xsl:with-param name="path" select="@name"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="fileCount" select="count(.//file[count(./error[@source=$error])>0])"/>

        <xsl:if test="$fileCount > 0">
            <li class="folder">
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="concat($parent-folder,$new-name)"/>
                        <xsl:text>summary.html</xsl:text>
                    </xsl:attribute>
                    <xsl:value-of select="@name"/>
                </a>
                (files: <xsl:value-of select="$fileCount"/>, errors: <xsl:value-of select="count(.//error[@source=$error])"/>)
                <ul>
                    <xsl:apply-templates select="file" mode="error.file">
                        <xsl:with-param name="error" select="$error"/>
                        <xsl:with-param name="parent-folder" select="concat($parent-folder,$new-name)"/>
                    </xsl:apply-templates>
                    <xsl:apply-templates select="folder" mode="error.folder">
                        <xsl:with-param name="error" select="$error"/>
                        <xsl:with-param name="parent-folder" select="concat($parent-folder,$new-name)"/>
                    </xsl:apply-templates>
                </ul>
            </li>
        </xsl:if>
    </xsl:template>

    <xsl:template match="file" mode="error.file">
        <xsl:param name="error"/>
        <xsl:param name="parent-folder"/>
        <xsl:variable name="errorCount" select="count(./error[@source=$error])"/>

        <xsl:if test="$errorCount > 0">
            <li class="file">
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="concat($parent-folder,@path)"/>
                        <xsl:text>.html</xsl:text>
                    </xsl:attribute>
                    <xsl:value-of select="@path"/>
                </a>
                (errors: <xsl:value-of select="$errorCount"/>)
            </li>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>